function Map(options){
    var $this = this;
    this.map = undefined;
    this.geocoder = undefined;
    this.directionsDisplay = undefined;    
    this.options = $.extend(true, Map.defaults, options);
    this.setOptions = function(options){
	options = options || {};
	this.options = $.extend(true, this.options, options);
	return this;
    }
    this.init = function(){	
	if (!this.options.element) {
	    throw new Error("Елемент карты не определён");
	}
	if (!document.getElementById(this.options.element)) {
	    throw new Error("Нету елемента карты");
	}	
	this.geocoder = new google.maps.Geocoder();
	this.directionsDisplay = new google.maps.DirectionsRenderer();    	
	this.map = new google.maps.Map(document.getElementById(this.options.element), {
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	});        	
	this.map.setCenter(new google.maps.LatLng(this.options.latitude, this.options.longitude));
	this.map.setZoom(this.options.zoom);	
	this.marker = new google.maps.Marker({
	    map: this.map,
	    draggable: this.options.marker.draggable,
	    position: this.map.getCenter(),
	    icon : this.options.marker.icon
	});	
	google.maps.event.addListener(this.marker, 'drag', function(){	    
	    $this.submitPosition();
	});		
	google.maps.event.addListener(this.marker, 'dragend', function(){	    
	    $this.geocodePosition();
	});		
	if ($(this.options.latElement).length && $(this.options.longElement).length) {
	    $(this.options.latElement).val(this.map.getCenter().lat());
	    $(this.options.longElement).val(this.map.getCenter().lng());
	}
	return this;
    }       
    this.submitPosition = function(){	
	if (this.marker !== undefined) {
	    var position = this.marker.getPosition();
	    if ($(this.options.latElement).length && $(this.options.longElement).length) {
		$(this.options.latElement).val(position.lat());
		$(this.options.longElement).val(position.lng());		
	    }
	}
    }
    this.displayCoords = function(lat, lng) {
	if (this.map !== undefined) {
	    this.map.setCenter(new google.maps.LatLng(lat, lng));
	    this.map.setZoom(this.options.zoom);
	    this.marker.setPosition(this.map.getCenter());
	}
    }
    this.refresh = function(){
	if (this.map !== undefined) {
	    google.maps.event.trigger(this.map, 'resize');	    
	    this.displayCoords(this.map.getCenter().lat(), this.map.getCenter().lng());
	}	
    }
    this.geocodePosition = function(){
	if (this.geocoder === undefined || this.marker === undefined) {
	    return;
	}
	var position = this.marker.getPosition();
	this.geocoder.geocode({
	    latLng: position
	}, function(responses, status) {
	    if (status == google.maps.GeocoderStatus.OK) {
		if ($($this.options.addressElement).length) {
		    $($this.options.addressElement).val(responses[0].formatted_address);		    
		}
	    }	    
	});
    }
    
    this.searchAndDisplay = function(address) {	
	if (this.geocoder === undefined) {
	    throw new Error("Geocoder не инициализирован");	    
	    return;
	}
	this.geocoder.geocode({
	    address: address
	},
	function(results, status) {	    
	    if (status == google.maps.GeocoderStatus.OK) {
		$this.map.setCenter(results[0].geometry.location);
		$this.map.setZoom($this.options.zoom);		
		$this.marker.setPosition($this.map.getCenter());
		if ($($this.options.latElement).length && $($this.options.longElement).length) {
		    $($this.options.latElement).val($this.map.getCenter().lat());
		    $($this.options.longElement).val($this.map.getCenter().lng());		    
		}		
		if ($($this.options.addressElement).length) {
		    $($this.options.addressElement).val(results[0].formatted_address);		    
		}
		
	    } else {
		showStatusMessage("Невозможно найти адрес", "warning");		
	    }
	}
	);
    }
    
       
}
Map.defaults = {
    latitude : "49.8427654643",
    longitude : "24.0320290642",
    latElement : "#latitude",
    longElement : "#longitude",
    addressElement : "#mapAddress",
    zoom : 15,
    element : "googleMap",
    marker : {
	icon : "/themes/miky/images/icons/marker.png",
	draggable : true
    }
};

